const mongoose = require("mongoose");
const rp = require("request-promise")
const add = async (distribution) => {
    const distributionSchema = mongoose.model("shop-distribution");
    return await distributionSchema.create(distribution)
}
const get = async ({ curpage, eachPage }) => {
    const result = {
        curpage: ~~curpage,
        eachPage: ~~eachPage,
        total: 0,
        maxPage: 0,
        rows: []
    }
    const distributionModel = mongoose.model("shop-distribution")
    result.total = await distributionModel.countDocuments();
    result.maxPage = Math.ceil(result.total / result.eachPage)
    result.rows = await distributionModel
        .find()
        .skip((result.curpage - 1) * result.eachPage)
        .limit(result.eachPage)
        .exec()
    return result
}
const xiu = async ({ _id, phone, address }) => {
    const modeldistribution = mongoose.model("shop-distribution").updateOne({ _id: _id },
        {
            $set: {
                phone: phone,
                address: address,
            }
        })
    return await modeldistribution;
}
const cha = async ({ name }) => {
    const distributionModel = mongoose.model("shop-distribution");
    return await distributionModel.find({
        name: {
            $regex: String(name)
        },
    });
}
const shan = async ({ _id }) => {
    const modeldistribution = mongoose.model("shop-distribution").deleteOne({ _id: _id })
    return await modeldistribution;
}
module.exports = { add, get, xiu,cha,shan }